let ol = document.getElementsByClassName("ol")[0];
let userId = localStorage.getItem("userId");

//网页自适应
window.onload = function () {
    function setHtml() {
        let cWidth =
            document.body.clientWidth || document.documentElement.clientWidth;
        let target = cWidth / 19;
        /*
          在375分辨率下，设置html根节点的font-size为100
          375/20 = 414/target(在414分辨率下的html font-size值)
          target=414/375*100
        */
        document.documentElement.style.fontSize = target + "px";
    }
    setHtml();
    window.onresize = setHtml;
}
//获取当前的id信息
function btnuserId() {
    if (userId) {
        let xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                ol.innerHTML = ` <li class="lis">
                              <img src="${xhr.responseText}" alt="">
                          </li>
                          <li class="lis" onclick = "out()">
                              👈退出登录
                          </li>`
            }
        }
        xhr.open("GET", `/api/admin/tx?id=${userId}`, true)
        xhr.send()
    }
    if (userId != "1") {
        document.getElementById("hide").style.display = "none";
    }
}
btnuserId();

//退出登录
function out() {
    clearCookie()
    localStorage.removeItem("userId");
    ol.innerHTML = ` <li class="li">
                          <a href="./html/login.html"><span>登录</span></a>
                      </li>
                      <li class="li">
                          <a href="./html/register.html"><span>注册</span></a>
                      </li>`
    location.href = "../index.html"
}


//清空Cookie函数
function clearCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if (keys) {
        for (var i = keys.length; i--;) {
            document.cookie =
                keys[i] + "=0;path=/;expires=" + new Date(0).toUTCString(); //清除当前域名下的,例如：m.kevis.com
            document.cookie =
                keys[i] +
                "=0;path=/;domain=" +
                document.domain +
                ";expires=" +
                new Date(0).toUTCString(); //清除当前域名下的，例如 .m.kevis.com
            document.cookie =
                keys[i] +
                "=0;path=/;domain=kevis.com;expires=" +
                new Date(0).toUTCString(); //清除一级域名下的或指定的，例如 .kevis.com
        }
    }
}

//防止有人修改localStorage
window.addEventListener("storage", (e) => {
    localStorage.setItem(e.key, e.oldValue)
})

//禁止浏览器放大或缩小
var t = window.devicePixelRatio
document.body.style.zoom = 1 / t

document.addEventListener('keydown', function (event) {
    if ((event.ctrlKey === true || event.metaKey === true) &&
        (event.which === 61 || event.which === 107 || event.which === 173 || event.which === 109 ||
            event.which === 187 || event.which === 189)) {
        event.preventDefault()
    }
}, false)

// Chrome IE 360
window.addEventListener('mousewheel', function (event) {
    if (event.ctrlKey === true || event.metaKey) {
        event.preventDefault()
    }
}, {
    passive: false
})

// firefox
window.addEventListener('DOMMouseScroll', function (event) {
    if (event.ctrlKey === true || event.metaKey) {
        event.preventDefault()
    }
}, {
    passive: false
})